using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Xml;
using System.Xml.XPath;
using System.Collections;
using System.Configuration;

//AUI Model Component
using AdaptiveHelpDesk.AUI_Model_Components.Watcher.Combo_box;

namespace AdaptiveHelpDesk
{
    class UpdateUserModelComboBox
    {

        #region VARIABLES

        SqlConnection con = null;
        SqlDataReader rdr = null;
        XmlDocument doc;

        XmlElement InteractionFeatures;

        XmlElement LowlevelMotionCharact;

        XmlElement InteractionTechn;

        XmlElement PerformanceM;

        String UserID = "";
        String dateLogged = "";
        String timeLogged = "";


        LowLevelMotionCharacteristics classLowLevelMotionCharacteristics;
        InteractionTechnique classInteractionTechnique;
        PerformanceModels classPerformaceModel;

        String getStoredProcedureColumn = "";
        String getStoreProcedureName = "";
        String updateStoredProcedureColumn = "";
        String updateStoreProcedureName = "";

        #endregion



        #region INITIALISATION

            public UpdateUserModelComboBox(string UID, string dateL, string timeL, string getSPName, string getSPColumn, string updateSPName, string updateSPColumn)
            {

                UserID = UID;
                dateLogged = dateL;
                timeLogged = timeL;
                getStoredProcedureColumn = getSPColumn;
                getStoreProcedureName = getSPName;
                updateStoredProcedureColumn = updateSPColumn;
                updateStoreProcedureName = updateSPName;

                //Get the User Model for the specified INformative Moment
                this.getUserModelComboBoxXML(UID, dateL, timeL);

                classLowLevelMotionCharacteristics = new LowLevelMotionCharacteristics();
                classInteractionTechnique = new InteractionTechnique();
                classPerformaceModel = new PerformanceModels();

                InteractionFeatures = (XmlElement)doc.DocumentElement.FirstChild;

                LowLevelMotionCharacteristics();

                InteractionTechnique();

                PerformanceModel();

            }

        #endregion



        #region OPEN CONNECTION

            private void openConnection()
            {
                ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings[3];

                try
                {
                    if (settings != null)
                    {
                        con = new SqlConnection(settings.ConnectionString);
                        con.Open();
                    }
                }
                catch (Exception ex)
                {                    
                    throw;
                }
            }

            #endregion

        
    
        #region GET USER MODEL XML SO THAT IT CAN BE UPDATED

                private XmlDocument getUserModelComboBoxXML(string UID, string dateLog, string timeLog)
                {
                    try
                    {
                        openConnection();
                        SqlCommand cmd = new SqlCommand(getStoreProcedureName, con);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add(new SqlParameter("@userID", UID));
                        cmd.Parameters.Add(new SqlParameter("@date", dateLog));
                        cmd.Parameters.Add(new SqlParameter("@time", timeLog));

                        rdr = cmd.ExecuteReader();
                        doc = new XmlDocument();
                        String XMLstring = "";

                        while (rdr.Read())
                        {
                            XMLstring = (String)rdr[getStoredProcedureColumn];
                        }

                        doc.LoadXml(XMLstring);

                        return doc;
                    }
                    finally
                    {
                        if (con != null)
                        {
                            con.Close();
                        }
                        if (rdr != null)
                        {
                            rdr.Close();
                        }
                    }
                }

            #endregion



        #region UPDATE USER MODEL XML

            public void updateUserModelComboBoxXML()
            {
                try
                {
                    openConnection();
                    SqlCommand cmd = new SqlCommand(updateStoreProcedureName, con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@userID", UserID));
                    cmd.Parameters.Add(new SqlParameter("@date", dateLogged));
                    cmd.Parameters.Add(new SqlParameter("@time", timeLogged));
                    cmd.Parameters.Add(new SqlParameter(updateStoredProcedureColumn, doc.InnerXml));
                    
                    cmd.ExecuteNonQuery();
                 }
                 finally
                 {
                   if (con != null)
                   {
                      con.Close();
                   }
                   if (rdr != null)
                   {
                      rdr.Close();
                   }
                 }
            }

        #endregion



        #region UPDATE LOW-LEVEL MOTION CHARACTERISTICS

            private void LowLevelMotionCharacteristics()
            {
                LowlevelMotionCharact = (XmlElement)InteractionFeatures.GetElementsByTagName("Low-levelMotionCharacteristics")[0];
            }

                public void updateTotalTime(decimal tTime)
                {
                    LowlevelMotionCharact.ChildNodes[0].InnerText = tTime.ToString();
                    this.updateUserModelComboBoxXML();
                }

                public void updateYMouseVelocity(int yMouseV)
                {
                    LowlevelMotionCharact.ChildNodes[1].InnerText = yMouseV.ToString();
                    this.updateUserModelComboBoxXML();
                }

                public void updateYMouseAcceleration(int yMouseA)
                {
                    LowlevelMotionCharact.ChildNodes[2].InnerText = yMouseA.ToString();
                    this.updateUserModelComboBoxXML();
                }

                public void updateDwellTime(decimal dwellTime)
                {
                    LowlevelMotionCharact.ChildNodes[3].InnerText = dwellTime.ToString();
                    this.updateUserModelComboBoxXML();
                }

        #endregion



        #region UPDATE INTERACTION TECHNIQUE

                private void InteractionTechnique()
                {
                    InteractionTechn = (XmlElement)InteractionFeatures.GetElementsByTagName("InteractionTechnique")[0];
                }

                public void updateAverageDwellTime(int tDTime)
                {
                    InteractionTechn.ChildNodes[0].InnerText = tDTime.ToString();
                    this.updateUserModelComboBoxXML();
                }

                public void updateNrItemsVisited(int nrItems)
                {
                    InteractionTechn.ChildNodes[1].InnerText = nrItems.ToString();
                    this.updateUserModelComboBoxXML();
                }

                public void updateUniqueItemsVisited(int nrUniqueItems)
                {
                    InteractionTechn.ChildNodes[2].InnerText = nrUniqueItems.ToString();
                    this.updateUserModelComboBoxXML();
                }

                public void updateSelectionTime(int sTime)
                {
                    InteractionTechn.ChildNodes[3].InnerText = sTime.ToString();
                    this.updateUserModelComboBoxXML();
                }

        #endregion


        
        #region UPDATE PERFORMANCE MODEL

                private void PerformanceModel()
                {
                    PerformanceM = (XmlElement)InteractionFeatures.GetElementsByTagName("PerformanceModels")[0];
                }

                public void updateKLMDifference(double KLMDiff)
                {
                    PerformanceM.ChildNodes[0].InnerText = KLMDiff.ToString();
                    this.updateUserModelComboBoxXML();
                }

                public void updateKLMRatio(double KLMRatio)
                {
                    PerformanceM.ChildNodes[1].InnerText = KLMRatio.ToString();
                    this.updateUserModelComboBoxXML();
                }

        #endregion


    }
}
